package com.anhuanjia.tools.eao.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.anhuanjia.bean.MhResource;
import com.anhuanjia.tools.eao.bean.EaoHelpContent;
import com.anhuanjia.tools.eao.bean.EaoHelpMenu;
import com.anhuanjia.tools.eao.bean.EaoImages;
import com.anhuanjia.tools.eao.bean.EaoMechanismInfo;
import com.anhuanjia.tools.eao.bean.EaoMenu;
import com.anhuanjia.tools.eao.bean.EaoOpinion;
import com.anhuanjia.tools.eao.bean.EaoPosition;
import com.anhuanjia.tools.eao.bean.EaoPositionMenu;
import com.anhuanjia.tools.eao.bean.EaoPublicityAudit;
import com.anhuanjia.tools.eao.bean.EaoVersion;
import com.anhuanjia.tools.eao.bean.EaoWeChatPublic;
import com.anhuanjia.tools.eao.beanvo.EaoMemberVo;
import com.anhuanjia.tools.eao.util.FindDateUtil;
import com.efastsoft.bean.EcMember;
import com.efastsoft.framework.Member;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.UserInfo;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;
import com.efastsoft.framework.ui.control.CommonDataGridAction;
import com.efastsoft.framework.utility.StringUtil;

@Alias("EaoGrid")
public class EaoUI extends UIFacade {

	/**
	 * 查询一个月操作记录
	 */
	@Priv(loginType = LoginType.ALL)
	public void getOneOper(CommonDataGridAction dga) {
		String sortField = $V("sortField");
		String sortOrder = $V("sortOrder");
		Q q = new Q(
				" select eol.*from eao_operation_log as eol left join Mh_Resource as mr on eol.MemberId=mr.Member left join  eao_is_engineer as en on mr.Member = en.Member left join  eao_Position as po on en.Member = po.Id where eol.CreateTime>DATE_SUB(CURDATE(), INTERVAL 1 MONTH)");
		if (StringUtil.isNotEmpty(sortField) && StringUtil.isNotEmpty(sortOrder)) {
			q.orderby(sortField + " " + sortOrder);
		} else {
			q.orderby("eol.CreateTime desc");
		}
		DataTable dt = q.fetch(50, 0);
		if (dt.getRowCount() != 0) {
			dga.bindData(dt);
			dga.setTotal(q);
		} else {
			$F("查询失败");
		}
	}

	/**
	 * 查询半年操作记录（更多）
	 */
	@Priv(loginType = LoginType.ALL)
	public void getOper(CommonDataGridAction dga) {
		String sortField = $V("sortField");
		String sortOrder = $V("sortOrder");
		String CompanyMobile = $V("CompanyMobile");
		String Summary = $V("Summary");
		String MemberSearch = $V("MemberSearch");
		String StartTime = $V("StartTime");
		String EndTime = $V("EndTime");
		Q q = new Q("select eol.* from eao_operation_log eol left join crm_company cc on eol.CompanyId = cc.ID where 1 = 1");
		if(StringUtil.isNotEmpty(CompanyMobile)){
			q.and().append(" (cc.FullName like ? or eol.CompanyMobile like ?)","%"+CompanyMobile+"%","%"+CompanyMobile+"%");
		}
		if(StringUtil.isNotEmpty(Summary)){
			q.and().like("eol.Summary", Summary);
		}
		if(StringUtil.isNotEmpty(MemberSearch)){
			q.and().append(" (eol.MemberMobile like ? or eol.MemberName like ?)","%"+MemberSearch+"%","%"+MemberSearch+"%");
		}
		if(StringUtil.isNotEmpty(StartTime)){
			q.and().append(" DATE_FORMAT(eol.CreateTime,'%Y-%m-%d')>=?", StartTime);
		}
		if(StringUtil.isNotEmpty(EndTime)){
			q.and().append(" DATE_FORMAT(eol.CreateTime,'%Y-%m-%d')<=?", EndTime);
		}
		if (StringUtil.isNotEmpty(sortField) && StringUtil.isNotEmpty(sortOrder)) {
			q.orderby(sortField + " " + sortOrder);
		} else {
			q.orderby("eol.CreateTime desc");
		}
		DataTable dt = q.fetch(dga);
		if (dt.getRowCount() != 0) {
			dga.bindData(dt);
			dga.setTotal(q);
			s("查询成功");
		} else {
			f("查询失败");
		}
	}

	/**
	 * 查询所有的反馈与支持数据
	 */
	@Priv(loginType = LoginType.ALL)
	public void allOpinions(CommonDataGridAction dga) {
		String sortField = $V("sortField");
		String sortOrder = $V("sortOrder");
		String SearchText = $V("SearchText");
		String StartTime = $V("StartTime");
		String EndTime = $V("EndTime");
		Q q = new Q(" select op.Id,co.FullName,coalesce(mh.Tel,mh.Mobile) as Tel,mh.Mobile,mh.RealName,op.Content,op.CreateTime,em.Mobile CompanyMobile from eao_opinion as op left join ec_member as mh on op.MemberId = mh.ID left join mh_resource mr on mh.ID = mr.Member left join crm_company as co on mh.ID = co.ID or co.ID = MR.Company left join ec_member as em on co.ID = em .ID where op.IsDelete = 0 ");
		
		if(StringUtil.isNotEmpty(SearchText)){
			if("非体系内账号".equals(SearchText)){
				q.and().append(" op.MemberId not in (select ID from crm_company) and op.MemberId not in (select Member from mh_resource)");
			}else{
				q.and().append(" ( mh.RealName like ? or mh.Mobile like ? or co.FullName like ? or em.Mobile like ?)", "%"+SearchText+"%","%"+SearchText+"%","%"+SearchText+"%","%"+SearchText+"%");
			}
			
		}
		if(StringUtil.isNotEmpty(StartTime)){
			q.and().append(" DATE_FORMAT(op.CreateTime,'%Y-%m-%d')>=?", StartTime);
		}
		if(StringUtil.isNotEmpty(EndTime)){
			q.and().append(" DATE_FORMAT(op.CreateTime,'%Y-%m-%d')<=?", EndTime);
		}
		if (StringUtil.isNotEmpty(sortField) && StringUtil.isNotEmpty(sortOrder)) {
			q.orderby("op."+sortField + " " + sortOrder);
		} else {
			q.orderby("op.CreateTime desc");
		}
		
		DataTable dt = q.fetch(dga);
		for (DataRow dataRow : dt) {
			if(0 == dataRow.getLong("mr.Member") && StringUtil.isEmpty(dataRow.getString("CompanyMobile")) ){
				dataRow.set("FullName", "非体系内账号");
				dataRow.set("CompanyMobile", "非体系内账号");
			}
		}
		dga.bindData(dt);
		dga.setTotal(q);
	}

	/**
	 * 删除数据
	 */
	@Priv(loginType = LoginType.ALL)
	public void deleteOpinion() {
		Transaction trans = new Transaction();
		String ids = $V("Ids");
		if (ids.endsWith(",")) {
			ids.substring(0, ids.length() - 1);
		}
		String[] id = ids.split(",");
		if (StringUtils.isEmpty(ids)) {
			f("操作失败，请确认操作信息!");
			return;
		}
		trans.add(new Q(" update eao_opinion  set IsDelete = 1 where ").in("Id", id));
		trans.add(new Q(" delete from eao_images where ").in("ParentId", id));
		if (trans.commit()) {
			s("删除成功");
		} else {
			f("删除失败");
		}

	}

	/**
	 * 查询一条反馈与支持数据
	 */
	@Priv(loginType = LoginType.ALL)
	public void findOneOpinion() {
		String id = $V("Id");
		if (StringUtils.isEmpty(id)) {
			f("操作失败，请确认操作信息!");
			return;
		}
		DataTable dt = new Q("select * from eao_opinion where Id = ? ", id).fetch();
		if (dt.getRowCount() != 0) {
			EaoOpinion opinion = new EaoOpinion();
			opinion.setValue(dt.get(0));
			DataTable dt_img = new Q("select * from eao_images where ParentId = ? ", id).fetch();
			List<EaoImages> imgList = new ArrayList<>();
			for (DataRow dataRow : dt_img) {
				EaoImages img = new EaoImages();
				img.setValue(dataRow);
				imgList.add(img);
			}
			this.Response.put("opinion", opinion);
			this.Response.put("imgList", dt_img);
			this.Response.put("imgSize", imgList.size());

		} else {
			f("查询失败,数据不存在!");
		}
	}

	/**
	 * 查看图片
	 */
	/**
	 * 查询一条反馈与支持数据
	 */
	@Priv(loginType = LoginType.ALL)
	public void findImage() {
		String id = $V("Id");
		if (StringUtils.isEmpty(id)) {
			f("操作失败，请确认操作信息!");
			return;
		}
		DataTable dt = new Q("select * from eao_images where Id = ? ", id).fetch();
		if (dt.getRowCount() != 0) {
			s("查询成功");
			this.Response.put("img", dt.get(0).toMapx());
		} else {
			f("查询失败,数据不存在!");
		}
	}

	/**
	 * 一级账号统计
	 * 
	 * @throws ParseException
	 */
	@Priv(loginType = LoginType.ALL)
	public void findAccountVisit(CommonDataGridAction dga) throws ParseException {
		String startTime = $V("StartTime");// 起始时间
		String endTime = $V("EndTime");// 结束时间
		String MProvince = $V("MProvince");// 省份
		String MCity = $V("MCity");// 城市
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		Date dBegin = null;
		Date dEnd = null;
		List<String> strList = new ArrayList<>();
		FindDateUtil findDateUtil = new FindDateUtil();
		Q qt = new Q("select count(id) from Ec_Member where Type = 'Ent'");
		Q q = new Q("select * from Ec_Member where Type = 'Ent'");
		Q qz = new Q("select DATE_FORMAT(AddTime,'%Y-%m-%d') , count(AddTime) from Ec_Member where Type = 'Ent'");
		if (StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			dEnd = sd.parse(endTime);
			dBegin = sd.parse(startTime);
			strList = findDateUtil.findDates(dBegin, dEnd);
			q.append(" and DATE_FORMAT(AddTime,'%Y-%m-%d') between ? and ?", startTime, endTime);
			qz.append(" and DATE_FORMAT(AddTime,'%Y-%m-%d') between ? and ?", startTime, endTime);
		} else if (StringUtil.isNotEmpty(startTime) && !StringUtil.isNotEmpty(endTime)) {
			Date dtEnd = new Date();
			endTime = sd.format(dtEnd);
			dEnd = sd.parse(endTime);
			dBegin = sd.parse(startTime);
			strList = findDateUtil.findDates(dBegin, dEnd);
			q.append(" and DATE_FORMAT(AddTime,'%Y-%m-%d') between ? and ?", startTime, endTime);
			qz.append(" and DATE_FORMAT(AddTime,'%Y-%m-%d') between ? and ?", startTime, endTime);
		} else if (!StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			dEnd = sd.parse(endTime);
			strList = findDateUtil.findDates(dBegin, dEnd);
			q.append(" and DATE_FORMAT(AddTime,'%Y-%m-%d') between DATE_SUB( ? , INTERVAL 1 MONTH) and ? ", endTime,
					endTime);
			qz.append(" and DATE_FORMAT(AddTime,'%Y-%m-%d') between DATE_SUB( ? , INTERVAL 1 MONTH) and ? ", endTime,
					endTime);
		} else if (!StringUtil.isNotEmpty(startTime) && !StringUtil.isNotEmpty(endTime)) {
			Date dtEnd = new Date();
			endTime = sd.format(dtEnd);
			dEnd = sd.parse(endTime);
			strList = findDateUtil.findDates(dBegin, dEnd);
			qz.append(" and DATE_FORMAT(AddTime,'%Y-%m-%d') > DATE_SUB( CURDATE() , INTERVAL 1 MONTH) ");
		}
		if (StringUtil.isNotEmpty(MProvince) && !StringUtil.isNotEmpty(MCity)) {
			q.append("and Address like ?", "%" + MProvince + "%");
			qz.append("and Address like ?", "%" + MProvince + "%");
		}
		if (!StringUtil.isNotEmpty(MProvince) && StringUtil.isNotEmpty(MCity)) {
			q.append("and Address like ?", "%" + MCity + "%");
			qz.append("and Address like ?", "%" + MCity + "%");
		}
		if (StringUtil.isNotEmpty(MProvince) && StringUtil.isNotEmpty(MCity)) {
			q.append("and Address like ?", "%" + MProvince + "%" + MCity + "%");
			qz.append("and Address like ?", "%" + MProvince + "%" + MCity + "%");
		}
		qz.append("GROUP BY DATE_FORMAT(AddTime,'%Y-%m-%d')");
		DataTable table = qz.fetch();
		DataTable dataTable = qt.fetch();
		DataTable dt = q.fetch(dga);

		String countId = dataTable.get(0, 0).toString();
		List<EaoMemberVo> list = new ArrayList<>();
		List<Map<String, String>> lists = new ArrayList<>();
		List<String> strs = new ArrayList<>();
		Map<String, String> mapOne = new HashMap<>();
		for (DataRow dataRow : table) {
			if (dataRow.getColumnCount() == 0) {
				f("该时间段内无一级账号");
			} else {
				for (String str : strList) {
					String AddTime = null;
					String count = "0";
					AddTime = dataRow.get(0).toString();
					count = dataRow.get(1).toString();
					if (str.equals(AddTime)) {
						mapOne.put("date", AddTime);
						mapOne.put(AddTime, count);
						strs.add(AddTime);
					}
				}
			}
		}
		for (String str : strList) {
			Map<String, String> map = new HashMap<>();
			Boolean lean = true;
			String count = "0";
			for (String mapTwo : strs) {
				if (str.equals(mapTwo)) {
					lean = false;
				}
			}
			if (lean) {
				map.put("date", str);
				map.put("count", count);
				lists.add(map);
			} else {
				map.put("date", str);
				map.put("count", mapOne.get(str));
				lists.add(map);
			}
		}
		if (dt.getRowCount() > 0) {
			for (DataRow dataRow : dt) {
				EaoMemberVo memberVo = new EaoMemberVo();
				EcMember member = new EcMember();
				member.setValue(dataRow);
				memberVo.setId(member.getID());
				memberVo.setCreate_time(member.getAddTime().toString());
				memberVo.setAddress(member.getAddress());
				DataTable dataTableTwo = new Q("select FullName from Crm_Company where ID = ?", member.getID()).fetch();
				memberVo.setFullName(dataTableTwo.getString(0, 0));
				list.add(memberVo);
			}
			this.Response.put("addTime", lists);// 数量——时间（折线图，柱状图）
			this.Response.put("countId", countId);// 统计一级账号总数
			this.Response.put("accountVisit", list.size());// 统计条件查询的一级账号数
			this.Response.put("member", list);
		} else {
			f("查询的账号不存在");
		}
	}

	/**
	 * 二级账号统计
	 * 
	 * @throws ParseException
	 */
	@Priv(loginType = LoginType.Member)
	public void findSubAccountVisit() throws ParseException {
		int pageSize = this.Request.getInt("PageSize");
		if (pageSize == 0) {
			pageSize = 10;
		}

		int pageIndex = this.Request.getInt("PageIndex");
		String startTime = $V("StartTime");// 起始时间
		String endTime = $V("EndTime");// 结束时间
		String Company = $V("Company");// 机构名称
		String IsEngineer = $V("JobTitle");// 职位
		String MProvince = $V("MProvince");// 省份
		String MCity = $V("MCity");// 城市
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		Date dBegin = null;
		Date dEnd = null;
		List<String> strList = new ArrayList<>();
		FindDateUtil findDateUtil = new FindDateUtil();
		Q qt = new Q("select count(*) from Mh_Resource");
		Q q = new Q("select * from Mh_Resource where 1 = 1 ");
		Q qz = new Q("select DATE_FORMAT(AddTime,'%Y-%m-%d') , count(AddTime) from Mh_Resource where 1 = 1 ");
		if (StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			dEnd = sd.parse(endTime);
			dBegin = sd.parse(startTime);
			strList = findDateUtil.findDates(dBegin, dEnd);
			q.append(" and DATE_FORMAT(AddTime,'%Y-%m-%d') between ? and ?", startTime, endTime);
			qz.append(" and DATE_FORMAT(AddTime,'%Y-%m-%d') between ? and ?", startTime, endTime);
		} else if (StringUtil.isNotEmpty(startTime) && !StringUtil.isNotEmpty(endTime)) {
			Date dtEnd = new Date();
			endTime = sd.format(dtEnd);
			dEnd = sd.parse(endTime);
			dBegin = sd.parse(startTime);
			strList = findDateUtil.findDates(dBegin, dEnd);
			q.append(" and DATE_FORMAT(AddTime,'%Y-%m-%d') between ? and ?", startTime, endTime);
			qz.append(" and DATE_FORMAT(AddTime,'%Y-%m-%d') between ? and ?", startTime, endTime);
		} else if (!StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			dEnd = sd.parse(endTime);
			strList = findDateUtil.findDates(dBegin, dEnd);
			q.append(" and DATE_FORMAT(AddTime,'%Y-%m-%d') > DATE_SUB( ? , INTERVAL 1 MONTH) ", endTime);
			qz.append(" and DATE_FORMAT(AddTime,'%Y-%m-%d') > DATE_SUB( ? , INTERVAL 1 MONTH) ", endTime);
		} else if (!StringUtil.isNotEmpty(startTime) && !StringUtil.isNotEmpty(endTime)) {
			Date dtEnd = new Date();
			endTime = sd.format(dtEnd);
			dEnd = sd.parse(endTime);
			strList = findDateUtil.findDates(dBegin, dEnd);
			qz.append(" and DATE_FORMAT(AddTime,'%Y-%m-%d') > DATE_SUB( CURDATE() , INTERVAL 1 MONTH) ");
		}
		if (StringUtil.isNotEmpty(MProvince)) {
			q.append("and MProvince like ?", MProvince + "%");
			qz.append("and MProvince like ?", MProvince + "%");
		}
		if (StringUtil.isNotEmpty(MCity)) {
			q.append("and MCity like ?", MCity + "%");
			qz.append("and MCity like ?", MCity + "%");
		}
		if (StringUtil.isNotEmpty(Company)) {
			q.append("and Company = ?", Company);
			qz.append("and Company = ?", Company);
		}
		if (StringUtil.isNotEmpty(IsEngineer)) {
			DataTable dataTable = new Q("select Member from eao_is_engineer where IsEngineer = ? ", IsEngineer).fetch();
			int i = 0;
			for (DataRow dataRow : dataTable) {
				if (i == 0 && dataTable.getRowCount() > 1) {
					q.append("and ( Member = ?", dataRow.get(0));
					qz.append("and ( Member = ?", dataRow.get(0));
				} else if (i < dataTable.getRowCount()) {
					q.append("or Member = ?", dataRow.get(0));
					qz.append("or Member = ?", dataRow.get(0));
				} else if (i == dataTable.getRowCount()) {
					q.append("or Member = ? )", dataRow.get(0));
					qz.append("or Member = ? )", dataRow.get(0));
				}
				i++;
			}

		}
		qz.append("GROUP BY AddTime");
		DataTable dt = q.fetch(pageSize, pageIndex);
		DataTable table = qz.fetch();
		DataTable dataTable = qt.fetch();
		if (dt.getRowCount() > 0) {
			String countId = dataTable.get(0, 0).toString();
			List<EaoMemberVo> list = new ArrayList<>();
			List<Map<String, String>> lists = new ArrayList<>();
			List<String> strs = new ArrayList<>();
			Map<String, String> mapOne = new HashMap<>();
			for (DataRow dataRow : table) {
				for (String str : strList) {
					String AddTime = null;
					String count = "0";
					AddTime = dataRow.get(0).toString();
					count = dataRow.get(1).toString();
					if (str.equals(AddTime)) {
						mapOne.put("date", AddTime);
						mapOne.put(AddTime, count);
						strs.add(AddTime);
					}
				}
			}
			for (String str : strList) {
				Map<String, String> map = new HashMap<>();
				Boolean lean = true;
				String count = "0";
				for (String mapTwo : strs) {
					if (str.equals(mapTwo)) {
						lean = false;
					}
				}
				if (lean) {
					map.put("date", str);
					map.put("count", count);
					lists.add(map);
				} else {
					map.put("date", str);
					map.put("count", mapOne.get(str));
					lists.add(map);
				}
			}
			for (DataRow dataRow : dt) {
				MhResource resource = new MhResource();
				resource.setValue(dataRow);
				EaoMemberVo memberVo = new EaoMemberVo();
				memberVo.setId(resource.getMember());
				memberVo.setCreate_time(resource.getAddTime().toString());
				if (StringUtil.isNotEmpty(resource.getMProvince()) && StringUtil.isNotEmpty(resource.getMCity())) {
					memberVo.setAddress(resource.getMProvince() + resource.getMCity());
				} else if (StringUtil.isNotEmpty(resource.getMProvince()) && StringUtils.isEmpty(resource.getMCity())) {
					memberVo.setAddress(resource.getMProvince());
				} else if (StringUtils.isEmpty(resource.getMProvince()) && StringUtils.isEmpty(resource.getMCity())) {
					memberVo.setAddress("");
				}
				memberVo.setName(resource.getFullName());
				DataTable dataTable3 = new Q(
						" select Position,ParentId from eao_Position where Id = (select IsEngineer from eao_is_engineer where Member = ?)",
						resource.getMember()).fetch();
				if (dataTable3.getRowCount() != 0) {
					EaoPosition position = new EaoPosition();
					position.setValue(dataTable3.get(0));
					if (StringUtils.isEmpty(position.getParentId())) {
						memberVo.setJobType(position.getPosition());
					} else {
						DataTable dataTableFour = new Q(" select Position from eao_Position where Id = ?",
								position.getParentId()).fetch();
						memberVo.setJobType(dataTableFour.getString(0, 0));
					}
				} else {
					memberVo.setJobType("游客");
				}
				if (memberVo.getAddress().isEmpty()) {
					memberVo.setAddress("");
				}
				String dataTableTwo = new Q("select FullName from Crm_Company where ID = ?", resource.getCompany())
						.executeString();
				if (!StringUtils.isEmpty(dataTableTwo)) {
					memberVo.setFullName(dataTableTwo);
				} else {
					memberVo.setFullName("");
				}
				list.add(memberVo);
			}
			this.Response.put("addTime", lists);// 数量——时间（折线图，柱状图）
			this.Response.put("countId", countId);
			this.Response.put("subAccountVisit", list.size());
			this.Response.put("member", list);
		} else {
			f("查询的账号不存在");
		}
	}

	/**
	 * 查询关于我们
	 */
	@Priv(loginType = LoginType.ALL)
	public void selectMe() {
		Q mechanismInfo = new Q("select m.Id as Qid,m.EnterpriseName,m.Address,m.Phone,m.URL from eao_mechanism_info as m");
		Q version = new Q("select v.Id as Bid,v.Version,v.VersionExplain,v.SoftwareUserAgreement from eao_version as v");
		Q wechatPublicAddress = new Q("select w.Id as Wid,w.Title,w.Content from eao_wechat_public_address as w ");
		
		DataTable mechanismInfoDt = mechanismInfo.fetch();
		DataTable versionDt = version.fetch();
		DataTable wechatPublicAddressDt = wechatPublicAddress.fetch();
		
		if (wechatPublicAddressDt.getRowCount() > 0) {
			Q wimg = new Q("select ImgUrl,ParentId as Id,CreateTime from eao_images where 1=1").and()
					.eq("ParentId", wechatPublicAddressDt.get(0).getString("Wid")).orderby("CreateTime desc");
			this.Response.putAll(wechatPublicAddressDt.get(0).toMapx());
			if (wimg.fetch(1, 0).getRowCount() > 0) {
				this.Response.put("wimg", wimg.fetch(1, 0).get(0).toMapx());
			} else {
				this.Response.put("wimg", "");
			}
		} 
		
		if(versionDt.getRowCount()>0){
			Q bimg = new Q("select ImgUrl,ParentId as Id,CreateTime from eao_images where 1=1").and()
					.eq("ParentId", versionDt.get(0).getString("Bid")).orderby("CreateTime desc");
			this.Response.putAll(versionDt.get(0).toMapx());
			if (bimg.fetch(1, 0).getRowCount() > 0) {
				this.Response.put("bimg", bimg.fetch(1, 0).get(0).toMapx());
			} else {
				this.Response.put("bimg", "");
			}
		}
		
		if(mechanismInfoDt.getRowCount()>0){
			this.Response.putAll(mechanismInfoDt.get(0).toMapx());
		}
	}

	/**
	 * 添加/更改内容表
	 */
	@Priv(loginType = LoginType.ALL)
	public void saveOrUpdateCompany() {
		String loginUser = Long.toString(Member.getUID());
		Date data = new Date();
		Transaction transaction = new Transaction();
		EaoMechanismInfo entity = new EaoMechanismInfo();
		entity.setValue(this.Request);
		String id = entity.getId();
		if (StringUtils.isEmpty(id)) {
			entity.setId(UUID.randomUUID().toString());
			entity.setCreateTime(data);
			entity.setUpdateTime(data);
			entity.setIsDelete("0"); // 默认0否
			entity.setOperator(loginUser); // 关联登录人，暂时设置为空
			entity.setMemberId(loginUser);
			transaction.insert(entity);
			if (transaction.commit()) {
				s("添加成功");
			} else {
				f("添加失败");
			}
		} else {
			DataTable dt = new Q(" select * from eao_mechanism_info where Id = ?", id).fetch();
			EaoMechanismInfo old = new EaoMechanismInfo();
			old.setValue(dt.get(0));
			old.setUpdateTime(data);
			old.setOperator(loginUser); // 关联登录人，暂时设置为空
			entity.setMemberId(loginUser);
			old.setValue(this.Request);
			transaction.update(old);
			if (transaction.commit()) {
				s("修改成功");
			} else {
				f("修改失败");
			}
		}
	}

	@Priv(loginType = LoginType.ALL)
	public void saveOrUpdateWx() {
		String loginUser = "";
		loginUser = Long.toString(Member.getUID());
		Date data = new Date();
		Transaction transaction = new Transaction();
		EaoWeChatPublic entity = new EaoWeChatPublic();
		entity.setValue(this.Request);
		String id = entity.getId();
		if (StringUtils.isEmpty(id)) {
			entity.setId(UUID.randomUUID().toString());
			entity.setCreateTime(data);
			entity.setUpdateTime(data);
			entity.setIsDelete(0); // 默认0否
			entity.setOperator(loginUser); // 关联登录人
			transaction.insert(entity);
			if (transaction.commit()) {
				s("添加成功");
			} else {
				f("添加失败");
			}
		} else {
			DataTable dt = new Q(" select * from eao_wechat_public_address where Id = ?", id).fetch();
			EaoWeChatPublic old = new EaoWeChatPublic();
			old.setValue(dt.get(0));
			old.setValue(this.Request);
			old.setUpdateTime(data);
			old.setOperator(loginUser); // 关联登录人
			transaction.update(old);
			if (transaction.commit()) {
				s("修改成功");
			} else {
				f("修改失败");
			}
		}
	}

	@Priv(loginType = LoginType.ALL)
	public void saveOrUpdateVersion() {
		String loginUser = Long.toString(Member.getUID());
		Date data = new Date();
		Transaction transaction = new Transaction();
		EaoVersion entity = new EaoVersion();
		entity.setValue(this.Request);
		String id = entity.getId();
		if (StringUtils.isEmpty(id)) {
			String uid = UUID.randomUUID().toString();
			entity.setId(uid);
			entity.setMemberId(loginUser); // 暂时设置为空
			entity.setCreateTime(data);
			entity.setUpdateTime(data);
			entity.setIsDelete(0); // 默认0否
			entity.setOperator(loginUser); // 关联登录人
			transaction.insert(entity);
			if (transaction.commit()) {
				s("添加成功");
			} else {
				f("添加失败");
			}
		} else {
			DataTable dt = new Q("select * from eao_version where Id = ? ", id).fetch();
			EaoVersion old = new EaoVersion();
			old.setValue(dt.get(0));
			old.setValue(this.Request);
			old.setUpdateTime(data);
			old.setOperator(loginUser);// 关联登录人，暂时设置为空
			transaction.update(old);
			if (transaction.commit()) {
				s("修改成功");
			} else {
				f("修改失败");
			}
		}
	}

	@Priv(loginType = LoginType.ALL)
	public void findBy() {
		DataTable dt = new Q("select * from eao_publicity_audit ").fetch();
		EaoPublicityAudit publicityAudit = new EaoPublicityAudit();
		publicityAudit.setValue(dt.get(0));
		this.Response.put("publicityAudit", publicityAudit);
	}

	@Priv(loginType = LoginType.ALL)
	public void saveOrUpdateType() {
		String id = $V("Id");
		String is = $V("IsAudit");
		Transaction transaction = new Transaction();
		if (StringUtils.isEmpty(id)) {
			EaoPublicityAudit publicityAudit = new EaoPublicityAudit();
			publicityAudit.setId(UUID.randomUUID().toString());
			publicityAudit.setIsAudit(is);// 是否需要审核(0否1是)
			transaction.insert(publicityAudit);
		} else {
			DataTable dt = new Q("select * from eao_publicity_audit where Id = ? ", id).fetch();
			if (dt.getRowCount() > 0) {
				EaoPublicityAudit publicityAudit = new EaoPublicityAudit();
				publicityAudit.setValue(dt.get(0));
				publicityAudit.setIsAudit(is);// 是否需要审核(0否1是)
				transaction.update(publicityAudit);
			} else {
				EaoPublicityAudit publicityAudit = new EaoPublicityAudit();
				publicityAudit.setId(UUID.randomUUID().toString());
				publicityAudit.setIsAudit(is);// 是否需要审核(0否1是)
				transaction.insert(publicityAudit);
			}
		}
		if (transaction.commit()) {
			s("公示审核状态更新成功");
		} else {
			f("公示审核状态更新失败");
		}

	}

	/**
	 * 获取菜单
	 */
	@Priv(loginType = LoginType.ALL)
	public void getMenus(CommonDataGridAction dga) {
		String type = $V("Type");
		if (StringUtil.isNotEmpty(type)) {
			Q q = new Q();
			if ("1".equals(type)) {
				q = new Q("select * from eao_help_menu where Type = 1 order by Position asc ");// type=1系统概述
			}
			if ("2".equals(type)) {
				q = new Q("select * from eao_help_menu where Type = 2 order by Position asc ");// type=2操作帮助
			}
			DataTable dt = q.fetch();
			if (dt.getRowCount() > 0) {
				dga.bindData(dt);
				dga.setTotal(q);
				s("查询成功");
			} else {
				f("没有数据");
			}
		} else {
			f("type参数错误");
		}
	}

	/**
	 * 查看帮助菜单
	 */
	@Priv(loginType = LoginType.ALL)
	public void findHelpMenu() {
		String id = $V("Id");
		Q q = new Q("select * from eao_help_menu where Id = ?", id);
		DataTable dt = q.fetch();
		if (dt.getRowCount() > 0) {
			DataRow dr = dt.get(0);
			EaoHelpMenu hm = new EaoHelpMenu();
			hm.setValue(dr);
			this.Response.put("data", dr);
			s("查询成功");
		} else {
			f("查询失败");
		}
	}

	/**
	 * 删除帮助菜单
	 */
	@Priv(loginType = LoginType.ALL)
	public void delHelpMenu() {
		Transaction t = new Transaction();
		String id = $V("Id");
		Q q = new Q("select * from eao_help_menu where Id = ?", id);
		DataTable dt = q.fetch();
		if (dt.getRowCount() > 0) {
			DataRow dr = dt.get(0);
			EaoHelpMenu hm = new EaoHelpMenu();
			hm.setValue(dr);
			Q delMenu = new Q("delete from eao_help_menu where Id = ? or ParentId = ? ", id, id);
			t.add(delMenu);
			DataTable dtContent = new Q("select * from eao_help_content where ParentId = ?", id).fetch();
			if (dtContent.getRowCount() > 0) {
				Q delMenuContent = new Q("delete from eao_help_content where ParentId = ?", id);
				t.add(delMenuContent);
			}
			if (t.commit()) {
				s("删除成功");
			} else {
				f("删除失败");
			}
		} else {
			f("删除失败");
		}
	}

	@Priv(loginType = LoginType.ALL)
	public void setIsDelete() {
		String id = $V("Id");
		String is_show = $V("IsShow");
		Q q = new Q("select * from eao_help_menu where Id = ? or ParentId = ?", id, id);
		DataTable dt = q.fetch();

		if (dt.getRowCount() > 0) {
			for (DataRow dataRow : dt) {
				EaoHelpMenu hm = new EaoHelpMenu();
				hm.setValue(dataRow);
				hm.setIsShow(Integer.valueOf(is_show));
				hm.update();
			}
			s("设置成功");
		} else {
			f("操作失败");
		}
	}

	/**
	 * 添加/更改帮助菜单表
	 */
	@Priv(loginType = LoginType.ALL)
	public void saveOrUpdateHelpMenu() {
		Transaction transaction = new Transaction();
		String memberId = Long.toString(Member.getUID());
		String id = $V("Id");
		if (StringUtils.isEmpty(id)) {
			DataTable dt = new Q("select max(Id) as MId from eao_help_menu").fetch();
			EaoHelpMenu hc = new EaoHelpMenu();
			hc.setValue(this.Request);
			if (null == hc.getParentId()) {
				hc.setParentId(0);
			}
			hc.setId(dt.get(0).getInt("MId") + 1);
			hc.setCreateTime(new Date());
			hc.setOperator(memberId);
			transaction.insert(hc);
			if (transaction.commit()) {
				s("添加成功");
			} else {
				f("操作失败");
			}
		} else {
			Q q = new Q("select * from eao_help_menu where Id = ?", id);
			DataTable dt = q.fetch();
			EaoHelpMenu hc = new EaoHelpMenu();
			hc.setValue(dt.get(0));
			hc.setValue(this.Request);
			hc.setOperator(memberId);
			transaction.update(hc);
			if (transaction.commit()) {
				s("修改成功");
			} else {
				f("修改失败");
			}
		}
	}

	/**
	 * 添加/更改内容表
	 */
	@Priv(loginType = LoginType.ALL)
	public void addMenuContent() {
		Transaction transaction = new Transaction();
		String MemberId = Long.toString(UserInfo.getID());
		String ParentId = $V("ParentId");
		Q q = new Q("select * from eao_help_content where ParentId = ?", ParentId);
		DataTable dt = q.fetch();
		if (dt.getRowCount() == 0) {
			EaoHelpContent hc = new EaoHelpContent();
			hc.setValue(this.Request);
			hc.setId(UUID.randomUUID().toString());
			hc.setCreateTime(new Date());
			hc.setMemberId(MemberId);
			transaction.insert(hc);
			if (transaction.commit()) {
				s("添加成功");
			} else {
				f("操作失败");
			}
		} else {
			EaoHelpContent hc = new EaoHelpContent();
			hc.setValue(dt.get(0));
			hc.setValue(this.Request);
			hc.setUpdateTime(new Date());
			hc.setMemberId(MemberId);
			transaction.update(hc);
			if (transaction.commit()) {
				s("修改成功");
			} else {
				f("操作失败");
			}
		}
	}

	/**
	 * 查看系统概述
	 */
	@Priv(loginType = LoginType.ALL)
	public void findXTGS() {
		String Id = $V("Id");
		Q q = new Q("select * from eao_help_content where ParentId = ?", Id);
		DataTable dt = q.fetch();
		EaoHelpContent hc = new EaoHelpContent();
		if (dt.getRowCount() > 0) {
			DataRow dr = dt.get(0);
			hc.setValue(dr);
			s("查询成功");
		} else {
			f("查询失败");
		}
		this.Response.put("data", hc);
	}

	/**
	 * 查询菜单
	 */
	@Priv(loginType = LoginType.ALL)
	public void findMenu(CommonDataGridAction dga) {
		Q q = new Q(
				"select em.*, GROUP_CONCAT(ep.position) as Position, GROUP_CONCAT(ep.Id) as PositionId from eao_menu as em left join eao_position_menu as epm on epm.MenuId = em.MenuId left join eao_position as ep on epm.PositionId = ep.Id GROUP BY em.MenuId order by em.Level ");
		DataTable dt = q.fetch();
		dga.bindData(dt);
		dga.setTotal(q);
	}

	/**
	 * 查询菜单
	 */
	@Priv(loginType = LoginType.Member)
	public void Menu() {
		List<EaoMenu> menuList = new ArrayList<>();
		DataTable dataTable = new Q(" select * from Mh_Resource where Member = ?", Member.getUID()).fetch();
		Q q = new Q();
		if ("Ent".equals(Member.getType()) && dataTable.getRowCount() == 0) {
			q = new Q(
					"select * from  eao_menu where State = 1 and MenuId in(select MenuId from eao_position_menu where PositionId = 999 ) order by level");
		} else {
			String IsEngineer = new Q(
					" select ParentId from eao_Position where Id = (select IsEngineer from eao_is_engineer where Member = ?) ",
					Member.getUID()).executeString();
			if (StringUtils.isEmpty(IsEngineer)) {
				q = new Q(
						"select * from  eao_menu where State = 1 and MenuId in (select MenuId from eao_position_menu where  PositionId = (select IsEngineer from eao_is_engineer where Member = ?)) order by Level",
						Member.getUID());
			} else {
				q = new Q(
						"select * from  eao_menu where State = 1 and MenuId in (select MenuId from eao_position_menu where  PositionId = ?) order by Level" , IsEngineer);
			}
		}
		DataTable dt = q.fetch();
		if (dt.getRowCount() > 0) {
			for (DataRow dataRow : dt) {
				EaoMenu menu = new EaoMenu();
				menu.setValue(dataRow);
				menuList.add(menu);
			}
			List<EaoMenu> hmTree = buildTree(menuList, Long.valueOf(0));
			this.Response.put("menu", JSON.toJSON(hmTree));
			s("查询成功");
		} else {
			f("查询失败");
		}
	}

	public static List<EaoMenu> buildTree(List<EaoMenu> list, Long parentId) {
		List<EaoMenu> menus = new ArrayList<EaoMenu>();
		for (EaoMenu menu : list) {
			Long menuId = menu.getMenuId();
			Long pid = menu.getParentId();
			if (parentId.equals(pid)) {
				List<EaoMenu> menuLists = buildTree(list, menuId);
				menu.setChildren(menuLists);
				menus.add(menu);
			}
		}
		return menus;
	}

	/**
	 * 查询菜单
	 */
	@Priv(loginType = LoginType.ALL)
	public void findPosition() {
		String MenuId = $V("MenuId");
		Q q = new Q(
				"select ep.Id as id,ep.Position as text from eao_position as ep  where ep.ParentId is null order by ep.Id");
		Q roleMenu = new Q("select * from eao_position_menu where MenuId = ?", MenuId);
		DataTable dt = q.fetch();
		DataTable menu = roleMenu.fetch();
		String str = "";
		for (DataRow dataRow : menu) {
			str += dataRow.getInt("PositionId") + ",";
		}
		this.Response.put("info", dt);
		this.Response.put("value", str);
	}

	/**
	 * 修改角色菜单
	 */
	@Priv(loginType = LoginType.ALL)
	public void updatePositionMenu() {
		Transaction t = new Transaction();
		String MenuId = $V("MenuId");
		String PositionId = $V("PositionId");
		if (StringUtil.isNotEmpty(MenuId)) {
			String parentId = new Q(" select parentId from eao_menu where MenuId = ?", MenuId).executeString();
			Q q = new Q("delete from eao_position_menu where MenuId = ?", MenuId);
			t.add(q);
			if (StringUtil.isNotEmpty(PositionId)) {
				if (StringUtils.isEmpty(parentId) || "0".equals(parentId)) {
					DataTable dataTable = new Q(
							" select * from eao_position_menu where MenuId in ( select MenuId from eao_menu where ParentId = ?)",
							MenuId).fetch();
					String[] position = PositionId.split(",");
					for (String str : position) {
						Q q2 = new Q("insert into eao_position_menu values(?,?)", MenuId, str);
						t.add(q2);
					}
					for (DataRow dr : dataTable) {
						EaoPositionMenu menu = new EaoPositionMenu();
						Boolean bl = true;
						menu.setValue(dr);
						for (String str : position) {
							if (menu.getPositionId().toString().equals(str)) {
								bl = false;
							}
						}
						if (bl) {
							t.delete(menu);
						}
					}
				} else {
					DataTable dataTable = new Q(" select * from eao_position_menu where menuId = ? ", parentId).fetch();
					String[] position = PositionId.split(",");
					for (String str : position) {
						Q q2 = new Q("insert into eao_position_menu values(?,?)", MenuId, str);
						t.add(q2);
					}
					for (String str : position) {
						Boolean bl = true;
						for (DataRow dr : dataTable) {
							EaoPositionMenu menu = new EaoPositionMenu();
							menu.setValue(dr);
							if (menu.getPositionId().toString().equals(str)) {
								bl = false;
							}
						}
						if (bl) {
							Q q2 = new Q("insert into eao_position_menu values(?,?)", parentId, str);
							t.add(q2);
						}
					}
				}
			}
			if (t.commit()) {
				s("修改成功");
			} else {
				f("修改失败");
			}
		} else {
			f("参数错误");
		}
	}

	/**
	 * 查询一条菜单信息
	 */
	@Priv(loginType = LoginType.ALL)
	public void findOneMenu() {
		String id = $V("Id");
		if (StringUtil.isNotEmpty(id)) {
			Q q = new Q("select * from eao_menu where MenuId = ?", id);
			DataTable dt = q.fetch();
			if (dt.getRowCount() > 0) {
				this.Response.put("info", dt.get(0));
			} else {
				f("查询失败");
			}
		} else {
			f("参数有误");
		}
	}

	/**
	 * 修改添加菜单信息
	 */
	@Priv(loginType = LoginType.ALL)
	public void saveOneMenu() {
		String id = $V("Id");
		if (StringUtil.isEmpty(id)) {
			EaoMenu menu = new EaoMenu();
			menu.setValue(this.Request);
			if (menu.getParentId() == null) {
				menu.setParentId(Long.valueOf(0));
			}
			menu.setMenuId(new Date().getTime());
			menu.setCreateTime(new Date());
			if (menu.insert()) {
				s("添加成功");
			} else {
				f("添加失败");
			}

		} else {
			Q q = new Q("select * from eao_menu where MenuId = ?", id);
			DataTable dt = q.fetch();
			EaoMenu menu = new EaoMenu();
			menu.setValue(dt.get(0));
			menu.setValue(this.Request);
			menu.setUpdateTime(new Date());
			if (menu.update()) {
				s("修改成功");
			} else {
				f("修改失败");
			}
		}
	}

	/**
	 * 删除菜单信息
	 */
	@Priv(loginType = LoginType.ALL)
	public void delOneMenu() {
		Transaction t = new Transaction();
		String MenuId = $V("MenuId");
		if (StringUtil.isNotEmpty(MenuId)) {
			Q q = new Q("delete from eao_menu where MenuId=?", MenuId);
			t.add(q);
			if (t.commit()) {
				s("删除成功");
			} else {
				f("删除失败");
			}
		} else {
			f("参数有误");
		}
	}

	/**
	 * 菜单是否启用
	 */
	@Priv(loginType = LoginType.ALL)
	public void setState() {
		String MenuId = $V("MenuId");
		String State = $V("State");
		Q q = new Q("select * from eao_menu where MenuId = ?", MenuId);
		DataTable dt = q.fetch();
		if (dt.getRowCount() > 0) {
			for (DataRow dataRow : dt) {
				EaoMenu menu = new EaoMenu();
				menu.setValue(dataRow);
				menu.setState(Integer.valueOf(State));
				menu.update();
			}
			s("设置成功");
		} else {
			f("操作失败");
		}
	}
}
